package com.cqjtu.tto.config;

import cn.dev33.satoken.interceptor.SaAnnotationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver;
import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
public class SaTokenConfigure extends WebMvcConfigurationSupport {
    // 注册sa-token的注解拦截器，打开注解式鉴权功能
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor (new SaAnnotationInterceptor()).addPathPatterns ("/**");
//    }

    /**
     * 静态资源映射规则
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler ("/static/**").addResourceLocations ("classpath:/static/");

        // 解决静态资源无法访问
        registry.addResourceHandler ("/**").addResourceLocations ("classpath:/static/");
        registry.addResourceHandler ("doc.html").addResourceLocations ("classpath:/META-INF/resources/");
        // 解决swagger无法访问
        registry.addResourceHandler ("/swagger-ui.html").addResourceLocations ("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler ("/webjars/**").addResourceLocations ("classpath:/META-INF/resources/webjars/");


        super.addResourceHandlers (registry);
    }


    /**
     * 跨域配置
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 允许跨域访问的路径
        registry.addMapping ("/**")
                // 是否发送cookie
                .allowCredentials (true)
                // 允许头部设置
                .allowedHeaders ("*")
                // 允许跨域访问的源
                .allowedOrigins ("*")
                // 允许请求方法
                .allowedMethods ("*");
    }

    @Bean
    public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
        return new DeviceResolverHandlerInterceptor ();
    }

    @Bean
    public DeviceHandlerMethodArgumentResolver
    deviceHandlerMethodArgumentResolver() {
        return new DeviceHandlerMethodArgumentResolver ();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new DeviceResolverHandlerInterceptor ());
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new DeviceHandlerMethodArgumentResolver());
    }
}